package com.eh.ucentermember.controller;


import com.eh.commonutils.JwtUtils;
import com.eh.commonutils.R;
import com.eh.commonutils.vo.UcenterMemberOrder;
import com.eh.ucentermember.entity.Member;
import com.eh.ucentermember.entity.vo.RegisterVo;
import com.eh.ucentermember.service.MemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author erhuo
 * @since 2021-01-22
 */
@RestController
@RequestMapping("/ucentermember/member")
public class MemberController {

    @Autowired
    private MemberService memberService;

    // 登录
    @PostMapping("login")
    public R loginUser(@RequestBody Member member) {
        // 返回token值，使用JWT生成
        String token = memberService.login(member);
        return R.ok().data("token", token);
    }

    // 注册
    @PostMapping("register")
    public R registerUser(@RequestBody RegisterVo registerVo) {
        memberService.register(registerVo);
        return R.ok();
    }

    // 根据tocken获取用户信息
    @GetMapping("getMemberInfo")
    public R getMemberInfo(HttpServletRequest request) {
        // 调用JWT工具类的方法。根据request对象获取头信息，返回用户ID
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        Member member = memberService.getById(memberId);
        return R.ok().data("userInfo", member)
                .data("nickname", member.getNickname())
                .data("avatar", member.getAvatar());
    }

    // 查询某一天注册的人数
    @GetMapping("CountRegister/{day}")
    public R getRegister(@PathVariable("day") String day) {
        Integer count = memberService.countRegisterDay(day);
        return R.ok().data("countRegister", count);
    }

    //根据用户id获取用户信息  课程评论用
    @PostMapping("getUserInfoCourse/{id}")
    public UcenterMemberOrder getUserInfoCourse(@PathVariable("id") String id) {
        Member member = memberService.getById(id);
        //把member对象里面值复制给UcenterMemberOrder对象
        UcenterMemberOrder ucenterMemberOrder = new UcenterMemberOrder();
        BeanUtils.copyProperties(member,ucenterMemberOrder);
        return ucenterMemberOrder;
    }

}

